Title: SQL W3S (By-BKK) / 031 Union
Tags: Obsidian_to_Anki

Q: What does the SQL UNION operator do?
A: It combines the result sets of two or more SELECT statements and removes duplicate rows.

Q: True or False: The UNION operator keeps duplicate rows from the result sets.
A: False. UNION removes duplicates.

Q: What requirement must every SELECT statement in a UNION meet?
A: Each SELECT must have the same number of columns.

Q: Columns in each SELECT statement used with UNION must have what property?
A: Columns must have similar data types.

Q: True or False: The order of columns in SELECT statements using UNION must be the same.
A: True

Q: How does SQL UNION decide the column names in its result set?
A: Column names are usually taken from the first SELECT statement.

Q: Write the basic syntax for UNION in SQL.
A: SELECT column_name(s) FROM table1 UNION SELECT column_name(s) FROM table2;

Q: What does UNION ALL do differently from UNION?
A: UNION ALL includes duplicate rows in the result set.

Q: How do you select only distinct cities from two tables, Customers and Suppliers, using UNION?
A: SELECT City FROM Customers UNION SELECT City FROM Suppliers;

Q: How do you get German cities from both Customers and Suppliers tables using UNION?
A: SELECT City, Country FROM Customers WHERE Country='Germany' UNION SELECT City, Country FROM Suppliers WHERE Country='Germany';

Q: When should you use UNION ALL instead of UNION?
A: Use UNION ALL when you want to include duplicate rows.

Q: What is the function of AS Type in a UNION query combining Customers and Suppliers?
A: It creates a temporary column called “Type” indicating source (Customer or Supplier).

Q: How do you list all customers and suppliers in one result using UNION?
A: SELECT 'Customer' AS Type, ContactName, City, Country FROM Customers UNION SELECT 'Supplier', ContactName, City, Country FROM Suppliers;